package VIEWS;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JToolBar;
import javax.swing.JButton;
import java.awt.Font;
import javax.swing.ImageIcon;
import java.awt.event.ActionListener;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.Vector;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import com.toedter.calendar.JDateChooser;

import CONTROLLERS.BenhNhanControllers;
import CONTROLLERS.HoSoBAControllers;
import ModelEntityFW.BenhNhanEntity;
import ModelEntityFW.HosoBAEntity;
import ModelEntityFW.UserEntitys;
import Ultils.DateConverter;

import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;

public class HoSoBAAdd extends JFrame {

	private JPanel contentPane;
	private JTextField txtHSID;
	private JTextField txtNguoiTHS;
	private JDateChooser DateHS;
	private JComboBox BoxBHYT;
	private JComboBox  BoxBHXH;
    private static Connection m_objConnection;
	private static UserEntitys m_objSecurityContext;
	private static HosoBAEntity m_objHSBAEntity;
	private HoSoBAControllers m_objHSBAControllers;
	private JComboBox bnID;

	/**
	 * Launch the application.
	 */
	/*public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					HoSoBAAdd frame = new HoSoBAAdd();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}*/

	/**
	 * Create the frame.
	 */
	public HoSoBAAdd(Connection objConnection,UserEntitys objSecurity,HosoBAEntity objHSBA) {
		m_objConnection=objConnection;
		m_objSecurityContext=objSecurity;
		m_objHSBAEntity= new HosoBAEntity();
		m_objHSBAEntity=objHSBA;
		m_objHSBAControllers = new HoSoBAControllers(m_objConnection, m_objSecurityContext);
		
		
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 382, 374);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JToolBar toolBar = new JToolBar();
		toolBar.setBounds(0, 0, 481, 40);
		contentPane.add(toolBar);
		
		JButton btnNewButton = new JButton("New Button");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				m_objHSBAEntity.setNgayTaoHS(DateConverter.convertDateToString(DateHS.getDate(), "yyyy-MM-dd"));
				m_objHSBAEntity.setNguoiTaoHS(txtNguoiTHS.getText());
                if (BoxBHYT.getSelectedIndex() == 0) {
                    m_objHSBAEntity.setBHYT(1);
                } else {
                    m_objHSBAEntity.setBHYT(0);
                }
                
                if (BoxBHXH.getSelectedIndex() == 0) {
                    m_objHSBAEntity.setBHXH(1);
                } else {
                    m_objHSBAEntity.setBHXH(0);
                }
                
                m_objHSBAEntity.setBN_ID(bnID.getSelectedIndex()+1);

				if (m_objHSBAEntity.isbFlag()) {
					m_objHSBAEntity.setHoSoID(Integer.parseInt(txtHSID.getText()));
					boolean bResult = false;
					try {
						bResult = m_objHSBAControllers.ExecOfUpdateElement(m_objHSBAEntity);
					} catch (Exception e1) {
						JOptionPane.showMessageDialog(getParent(), "Err: " + e1, "Thông báo",
								JOptionPane.INFORMATION_MESSAGE);
					}
					if (bResult) {
						JOptionPane.showMessageDialog(getParent(), "Cập nhật thông tin bệnh nhân thành công!",
								"Thông báo", JOptionPane.INFORMATION_MESSAGE);
						
						dispose();
						m_objHSBAEntity.setbFlag(false);
					}
				} else {
					boolean bResult = false;
					try {
						bResult = m_objHSBAControllers.ExecOfInsertElement(m_objHSBAEntity);
					} catch (Exception e1) {
						JOptionPane.showMessageDialog(getParent(), "Err: " + e1, "Thông báo",
								JOptionPane.INFORMATION_MESSAGE);
					}
					if (bResult) {
						JOptionPane.showMessageDialog(getParent(), "Thêm bệnh nhân thành công!", "Thông báo",
								JOptionPane.INFORMATION_MESSAGE);
						dispose();
					}
				}

			
			}
		});
		btnNewButton.setIcon(new ImageIcon(HoSoBAAdd.class.getResource("/images/book_edit.png")));
		btnNewButton.setFont(new Font("Cambria", Font.PLAIN, 14));
		toolBar.add(btnNewButton);
		
		JButton btnThot = new JButton("Thoát");
		btnThot.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				dispose();
			}
		});
		btnThot.setIcon(new ImageIcon(HoSoBAAdd.class.getResource("/images/cancel.gif")));
		btnThot.setFont(new Font("Cambria", Font.PLAIN, 14));
		toolBar.add(btnThot);
		
		JLabel lblNewLabel = new JLabel("Hồ Sơ ID:");
		lblNewLabel.setFont(new Font("Cambria", Font.PLAIN, 14));
		lblNewLabel.setBounds(10, 62, 80, 25);
		contentPane.add(lblNewLabel);
		
		JLabel lblNgyToH = new JLabel("Ngày Tạo Hồ Sơ:");
		lblNgyToH.setFont(new Font("Cambria", Font.PLAIN, 14));
		lblNgyToH.setBounds(10, 100, 106, 25);
		contentPane.add(lblNgyToH);
		
		JLabel lblNgiToH = new JLabel("Người Tạo Hồ Sơ:");
		lblNgiToH.setFont(new Font("Cambria", Font.PLAIN, 14));
		lblNgiToH.setBounds(10, 138, 117, 25);
		contentPane.add(lblNgiToH);
		
		JLabel lblBoHimY = new JLabel("Bảo Hiểm Y Tế:");
		lblBoHimY.setFont(new Font("Cambria", Font.PLAIN, 14));
		lblBoHimY.setBounds(10, 178, 106, 25);
		contentPane.add(lblBoHimY);
		
		JLabel lblBoHimX = new JLabel("Bảo Hiểm Xã Hội:");
		lblBoHimX.setFont(new Font("Cambria", Font.PLAIN, 14));
		lblBoHimX.setBounds(10, 214, 117, 25);
		contentPane.add(lblBoHimX);
		
		JLabel lblBnid = new JLabel("BN_ID:");
		lblBnid.setFont(new Font("Cambria", Font.PLAIN, 14));
		lblBnid.setBounds(10, 250, 80, 25);
		contentPane.add(lblBnid);
		
		txtHSID = new JTextField();
		txtHSID.setEditable(false);
		txtHSID.setFont(new Font("Cambria", Font.PLAIN, 14));
		txtHSID.setColumns(10);
		txtHSID.setBounds(138, 61, 218, 26);
		contentPane.add(txtHSID);
		
		txtNguoiTHS = new JTextField();
		txtNguoiTHS.setFont(new Font("Cambria", Font.PLAIN, 14));
		txtNguoiTHS.setColumns(10);
		txtNguoiTHS.setBounds(137, 137, 219, 26);
		contentPane.add(txtNguoiTHS);
		
		DateHS = new JDateChooser();
		DateHS.setDateFormatString("dd/MM/yyyy");
		DateHS.setBounds(138, 100, 218, 25);
		contentPane.add(DateHS);
		
		BoxBHYT = new JComboBox();
		BoxBHYT.setModel(new DefaultComboBoxModel(new String[] {"Có", "Không"}));
		BoxBHYT.setBounds(138, 174, 70, 27);
		contentPane.add(BoxBHYT);
		
		BoxBHXH = new JComboBox();
		BoxBHXH.setModel(new DefaultComboBoxModel(new String[] {"Có", "Không"}));
		BoxBHXH.setBounds(137, 214, 70, 27);
		contentPane.add(BoxBHXH);
		
		bnID = new JComboBox(getItemBNID(m_objConnection));
		bnID.setBounds(138, 253, 218, 26);
		contentPane.add(bnID);
		
        if (m_objHSBAEntity.isbFlag()) {
            // Khi cập nhật
        	setTitle("---Cập nhật hồ sơ---");
        	btnNewButton.setText("Cập nhật");
            BindingControls(m_objHSBAEntity);
        }else{
        	setTitle("---Thêm mới hồ sơ---");
        	btnNewButton.setText("Thêm mới");
        }
	}
	
	public static Vector<String> getItemBNID(Connection objConnec){
		Vector<String> lisItem = new Vector<String>();
		
		ResultSet objRs = null;
		CallableStatement objCstm = null;
		
		try {
			objCstm = objConnec.prepareCall("{CALL SP_GET_BENHNHAN_INFO()}");
			
			objRs = objCstm.executeQuery();
			
			while(objRs.next()){
				lisItem.addElement(objRs.getString("HoVaTen"));
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.getMessage();
		}
		
		return lisItem;
	}
	
    void BindingControls(HosoBAEntity objHSBAEntity) {
    	txtHSID.setText(""+objHSBAEntity.getHoSoID());
    	DateHS.setDate(DateConverter.convertStringToDate(objHSBAEntity.getNgayTaoHS(), "yyyy-MM-dd"));
    	txtNguoiTHS.setText(objHSBAEntity.getNguoiTaoHS());
    	BoxBHYT.setSelectedIndex(objHSBAEntity.getBHYT());
    	BoxBHXH.setSelectedIndex(objHSBAEntity.getBHXH());
    	bnID.setSelectedIndex(objHSBAEntity.getBN_ID());
    	
    }
}
